import tool from "../../modules/tool"
import config from '../../config'

// components/imageSelector/imageSelector.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      default: []
    },
    count: {
      type: Number,
      default: 0
    },
    preview: {
      type: Boolean,
      default: false,
    },
    onChange: {
      type: Function,
      default: () => { }
    },
    fullURL: {
      type: Boolean,
      default: false
    },
    location1: {
      type: String,
      default: ''
    },
    location2: {
      type: String,
      default: ''
    },
    showMap: {
      type: Boolean,
      default: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    handlePreviewLocation(title = '') {
      tool.handleOpenPatrolLocation({
        location1: this.properties.location1,
        location2: this.properties.location2,
      }, title)
      wx.openLocation({
        latitude: parseFloat(this.properties.location1),
        longitude: parseFloat(this.properties.location2),
        fail(e) {
          console.log(e)
        }
      })
    },
    handleImageClick(e1) {
      e1 = e1.currentTarget.dataset
      if (this.properties.preview) {
        wx.previewImage({
          current: (this.properties.fullURL ? '' : config.imagePrefix) + this.properties.list[e1.index],
          urls: (() => {
            let t = tool.dcJSON(this.properties.list)
            if (this.properties.fullURL) {
              return t
            }
            for (let i = 0; i < t.length; i++)t[i] = `${config.imagePrefix}${t[i]}`
            return t
          })()
        })
        return
      }
      wx.showActionSheet({
        itemList: ['预览', '删除'],
        success: (e) => {
          e = e.tapIndex
          if (e === 0) {
            wx.previewImage({
              current: (this.properties.fullURL ? '' : config.imagePrefix) + this.properties.list[e1.index],
              urls: (() => {
                let t = tool.dcJSON(this.properties.list)
                if (this.properties.fullURL) {
                  return t
                }
                for (let i = 0; i < t.length; i++)t[i] = `${config.imagePrefix}${t[i]}`
                return t
              })()
            })
          }
          if (e === 1) {
            this.triggerEvent('change', [...this.properties.list.slice(0, e1.index), ...this.properties.list.slice(e1.index + 1)]);
          }
        }
      })
    },
    async handleSelectImage() {
      let re = await tool.chooseImages(this.properties.count - this.properties.list.length);
      let t = this.properties.list;
      this.triggerEvent('change', [...t, ...re]);
    }
  },

  attached() {
    console.log(this.properties.data)
  }
})